﻿package tools;
import java.lang.reflect.Array;
import java.lang.reflect.Method;

/** 反射 */
public class ReflectDemo {

	public static void main(String[] args) {
		int[] temp = { 1, 2, 3 };
		String[] strs = { "AA", "BB", "CC" };
		Method m = null;
		try {
			Class<?> c1 = Class.forName("com.javase.other.demo.Person");
			m= c1.getMethod("sayHello", String.class);
			/** 通过反射调用方法 */
			m.invoke(c1.newInstance(), "A");
			/** 通过反射调用setter/getter */
			Object obj = c1.newInstance();
			setter(obj, "name", "AAA", String.class);
			setter(obj, "age", 1 , int.class);
			System.out.println(obj);
			obj = getter(obj, "name");
			System.out.println("name:"+obj);
			/** 通过反射获取数组信息并通过 Array类操作数组 */
			Class<?> c2 = temp.getClass();
			if (c2.isArray()) {
				Class<?> c22 = c2.getComponentType();
				System.out.println("数组类型:" + c22.getName());
				System.out.print("数组元素：");
				//将第一个元素设置为1000
				Array.set(temp, 0, 1000);
				printArr(temp);
			}
			int[] newArr = (int[]) changeArrCapacity(temp, 6);
			String[] newStrs = (String[]) changeArrCapacity(strs, 9);
			printArr(newArr);
			printArr(newStrs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 改变原数组容量 */
	public static Object changeArrCapacity(Object obj, int newLen) {
		Class<?> c = obj.getClass();
		Object newArr = null;
		Class<?> c1 = null;
		if (c.isArray()) {
			c1 = c.getComponentType();
			/** 开辟新数组 */
			newArr = Array.newInstance(c1, newLen);
			/** 拷贝原始数据到新数组 */
			System.arraycopy(obj, 0, newArr, 0, Array.getLength(obj));
		} else {
			return null;
		}
		return newArr;
	}

	/** 打印数组 */
	public static void printArr(Object obj) {
		Class<?> c = obj.getClass();
		if (c.isArray()) {
			for (int i = 0; i < Array.getLength(obj); i++) {
				System.out.print(Array.get(obj, i));
				if (i < Array.getLength(obj) - 1) {
					System.out.print(",");
				}
			}
			System.out.println();
		} else {
			return;
		}
	}

	/** 大写首字母(用于拼凑setter/getter方法) */
	public static String getInitUpcaseStr(String str) {
		str = str.substring(0, 1).toUpperCase() + str.substring(1);
		return str;
	}

	/** setter方法,通过反射为对象设值 */
	public static void setter(Object obj, String arr, Object value,
			Class<?> type) {
		Method m = null;
		try {
			m = obj.getClass().getMethod("set" + getInitUpcaseStr(arr), type);
			m.invoke(obj, value);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** getter方法,通过反射获取对象值 */
	public static Object getter(Object obj, String arr) {
		Method m = null;
		Object result = null;
		try {
			m = obj.getClass().getMethod("get" + getInitUpcaseStr(arr));
			result = m.invoke(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}

class Person {
	String name;
	int age;

	public void sayHello(String name) {
		System.out.println("hi:" + name);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "name:" + name + "," + "age:" + age;
	}
}